package com.hthy.futures.ui;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import com.hthy.futures.R;

/**
 *
 * Author:  Mr.bingley
 * Version:
 * Date:  2017/4/18
 */

/**
 *
 *  how to use?
 *
 * if (orderBuyDialog == null) {
     orderBuyDialog = new OrderBuyDialog(getActivity());
     }
     orderBuyDialog.setCancelable(false);
     orderBuyDialog.setCanceledOnTouchOutside(true);    ------------奇怪为啥在子类中这个属性起不了作用
     orderBuyDialog.setOrderData(mOrderStr);
     orderBuyDialog.show();

   layout:
    android:background="@color/white"
 */

public abstract class BaseDialog extends Dialog {
    public BaseDialog(Context context) {
        this(context, R.style.quick_option_dialog);
    }

    public BaseDialog(Context context, int themeResId) {
        super(context, themeResId);
        View contentView = View.inflate(context, getContentView(), null);

        initView(contentView);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        super.setContentView(contentView);
    }

    @SuppressWarnings("deprecation")
    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        getWindow().setGravity(getDialogGravity());
        WindowManager m = getWindow().getWindowManager();
        Display d = m.getDefaultDisplay();
        WindowManager.LayoutParams p = getWindow().getAttributes();
        p.width = ViewGroup.LayoutParams.MATCH_PARENT;
        p.height = ViewGroup.LayoutParams.MATCH_PARENT;
        getWindow().setAttributes(p);

    }

    /**
     * 决定弹框的位置 ,默认是bottom 子类可复写。
     * @return
     */
    protected  int getDialogGravity() {
        return Gravity.BOTTOM;
    }

    /**
     * 初始化视图
     * @param contentView
     */
    protected abstract void initView(View contentView);

    /**
     * 设置layoutid
     * @return
     */
    protected abstract int getContentView();
}
